package com.yidu.ladder.delivery.service.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.ladder.base.domain.Staff;
import com.yidu.ladder.base.domain.Subarea;
import com.yidu.ladder.delivery.dao.BusinessRecsiptDao;
import com.yidu.ladder.delivery.domain.BusinessRecsipt;
import com.yidu.ladder.delivery.domain.BusinessRecsiptVo;
import com.yidu.ladder.delivery.domain.Workorder;
import com.yidu.ladder.delivery.service.BusinessRecsiptService;
import com.yidu.ladder.system.domain.Customer;
import com.yidu.ladder.util.IDGenerator;
import com.yidu.ladder.util.PinYin4jUtils;
import com.yidu.ladder.util.WorkOrderNewNo;


/**
 * 受理业务实现类
 * @author lw
 * @since 2020/1/10
 */
@Service
public class BusinessRecsiptServiceImpl implements BusinessRecsiptService{
	
	/**
	 * 保留对业务受理数据访问层的依赖注入
	 */
	@Autowired
	private BusinessRecsiptDao businessRecsiptDao;

	public BusinessRecsiptServiceImpl() {
		super();
	}

	public BusinessRecsiptServiceImpl(BusinessRecsiptDao businessRecsiptDao) {
		super();
		this.businessRecsiptDao = businessRecsiptDao;
	}

	public BusinessRecsiptDao getBusinessRecsiptDao() {
		return businessRecsiptDao;
	}

	public void setBusinessRecsiptDao(BusinessRecsiptDao businessRecsiptDao) {
		this.businessRecsiptDao = businessRecsiptDao;
	}
	
	@Override
	public int dateBusinessSizeQuery(String date) {
		return businessRecsiptDao.dateBusinessSizeQuery(date);
	}

	@Override
	public Map<String,Object> findAllBusinessRecsiptWithPage(BusinessRecsiptVo businessRecsiptVo) {
		//计算分页
		businessRecsiptVo.setPage((businessRecsiptVo.getPage() -1 ) * businessRecsiptVo.getLimit());
		//查询所有
		List<BusinessRecsipt> businessRecsiptList = businessRecsiptDao.findAllBusinessRecsiptWithPage(businessRecsiptVo);
		//计数
		int total = businessRecsiptDao.countBusinessRecsipt(businessRecsiptVo);
		//定义一个Map对象
		Map<String, Object> map = new HashMap<String, Object>();
		//将数据封装到结果映射中 
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", total);
		map.put("data", businessRecsiptList);
		return map;
	}
	
	@Override
	public Customer findCustomerByCustomerPhone(String customerPhone) {
		return businessRecsiptDao.findCustomerByCustomerPhone(customerPhone);
	}

	
	@Override
	public boolean addBusinessRecsipt(Map<String, Object> param) throws ParseException {
		//1.添加业务受理单
		//从map对象里拿到预约取件时间
		Object pickTime = param.get("pickTime");
		//从map对象里拿到省
		String province = (String) param.get("province");
		//从map对象里拿到市
		String city = (String) param.get("city");
		//从map对象里拿到县(区)
		String district = (String) param.get("district");
		//从map对象里拿到客户电话
		String customerPhone = (String) param.get("senderPhone");
		//从map对象里拿到取件地址
		String pickupAddress = (String) param.get("senderAddress");
		//从map对象里拿到收件人
		String recipients = (String) param.get("addressee");
		//从map对象里拿到收件人电话号码
		String recipientsPhone = (String) param.get("addresseePhone");
		//从map对象里拿到收件人地址
		String recipientsAddress = (String) param.get("addresseeAddress");
		//从map对象里拿到收件人城市
		String recipientsCity = (String) param.get("citys");
		//从map对象里拿到产品时限
		String product = (String) param.get("product");
		//从map对象里拿到重量
		BigDecimal weight = BigDecimal.valueOf(Double.parseDouble((String)param.get("weight")));
		//从map对象里拿到体积
		BigDecimal volume =  BigDecimal.valueOf(Double.parseDouble((String)param.get("volume")));
		//从map对象里拿到件数
		BigInteger numberOfPackages = new BigInteger((String)param.get("numberOfPackages"));
		
		//调用PinYin4j工具类的方法来获取省的首字母大写
        String[] provinces = PinYin4jUtils.getHeadByString(province);
        //调用PinYin4j工具类的方法来获取市的首字母大写
        String[] citys = PinYin4jUtils.getHeadByString(city);
        //调用PinYin4j工具类的方法来获取区(县)的首字母大写
        String[] districts = PinYin4jUtils.getHeadByString(district);
	 
        //定义空的字符串来接收省的简码
        String shortCodeP = "";
        //使用普通for循环来获取省的字母
        for(int i=0;i<provinces.length-1;i++){
        	//将处理过后的省简码放入到空的字符串中
        	shortCodeP = shortCodeP+provinces[i];
        }
        //定义空的字符串来接收市的简码
        String shortCodeC = "";
        //使用普通for循环来获取市的字母
        for(int i=0;i<citys.length-1;i++){
        	//将处理过后的市简码放入到空的字符串中
        	shortCodeC = shortCodeC+citys[i];
        }
        //定义空的字符串来接收区(县)的简码
        String shortCodeD = "";
        //使用普通for循环来获取区(县)的字母
        for(int i=0;i<districts.length-1;i++){
        	//将处理过后的区(县)简码放入到空的字符串中
        	shortCodeD = shortCodeD+districts[i];
        }
        //将省市县编码合成城市简码
        String pickupCity = shortCodeP+shortCodeC+shortCodeD;
        
		//将预约取件时间加2小时
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = dateFormat.parse((String) pickTime);
		Calendar addHour = Calendar.getInstance();
		addHour.setTime(date);
		addHour.add(Calendar.HOUR, 2);
		Date dateHour = addHour.getTime();
		//调用方法 根据编码查询分区
		Subarea subarea = businessRecsiptDao.findCityCodeByQuery(pickupCity);
		//定义定区Id
		String ripAreaId = subarea.getRipAreaId();
		//创建受理单对象
		BusinessRecsipt businessRecsipt = new BusinessRecsipt();
		//受理单Id
		businessRecsipt.setBusinessRecsiptId(IDGenerator.getId());
		//预约时间
		businessRecsipt.setPickTime(dateHour);
		//受理单号
		String businessRecsiptNo = WorkOrderNewNo.newNo("业务受理", businessRecsiptDao
				.dateBusinessSizeQuery(new SimpleDateFormat("yyMMdd").format(System.currentTimeMillis())));
		//受理单号赋值
		businessRecsipt.setBusinessRecsiptNo(businessRecsiptNo);
		//调用方法  根据电话号码查询客户
		Customer customer = businessRecsiptDao.findCustomerByCustomerPhone(customerPhone);
		//客户Id
		businessRecsipt.setCustomerId(customer.getCustomerId());
		//客户名
		businessRecsipt.setCustomerName(customer.getCustomerName());
		//联系人
		businessRecsipt.setContacts(customer.getContactsName());
		//联系人电话
		businessRecsipt.setContactsPhone(customer.getCustomerPhone());
		//取件地址
		businessRecsipt.setPickupAddress(pickupAddress);
		//取件城市
		businessRecsipt.setPickupCity(pickupCity);
		//收件人
		businessRecsipt.setRecipients(recipients);
		//收件人电话
		businessRecsipt.setRecipientsPhone(recipientsPhone);
		//收件人地址
		businessRecsipt.setRecipientsAddress(recipientsAddress);
		//收件人城市
		businessRecsipt.setRecipientsCity(recipientsCity);
		//产品时限
		businessRecsipt.setProduct(product);
		//重量
		businessRecsipt.setWeight(weight);
		//体积
		businessRecsipt.setVolume(volume);
		//件数
		businessRecsipt.setNumberOfPackages(numberOfPackages);
		//是否派单成功
		businessRecsipt.setOrderType("Y");
		//判断是否添加成功
		if( 1 == businessRecsiptDao.addBusinessRecsipt(businessRecsipt)) {
			boolean result = true;
			//创建工作单对象   生成工作单
			Workorder workorder = new Workorder();
			//派工单Id
			workorder.setWorkBillId(IDGenerator.getId());
			//工单编号
			String workBillNo = WorkOrderNewNo.newNo("派工单", businessRecsiptDao
					.dateBusinessSizeQuery(new SimpleDateFormat("yyMMdd").format(System.currentTimeMillis())));
			workorder.setWorkBillNo(workBillNo);
			//受理单ID
			workorder.setBusinessNoticeId(businessRecsipt.getBusinessRecsiptId());
			//受理单编号
			workorder.setBusinessNoticeNo(businessRecsipt.getBusinessRecsiptNo());
			//派工单类型
			workorder.setBillType(0);
			//取件状态
			workorder.setPickState(2);
			//短信序号
			workorder.setSsmNumber(0);
			//工单生产时间
			workorder.setWorkBillTime(new Date(System.currentTimeMillis()));
			//追单次数
			workorder.setTraceNum(0);
			//根据定区Id查询职员
			List<Staff> staffList = businessRecsiptDao.findStaffByRipAreaIdQuery(ripAreaId);
			/*
			 * 判断是否有符合条件的取派员;
			 * 有就向将该取派员添加到工单表中,没有将修改受理单状态
			 */
			if(staffList.size() == 0) {
				//修改受理单状态
				businessRecsiptDao.updateOrderTypeByBusinessRecsiptId(businessRecsipt.getBusinessRecsiptId());
				return false;
			}else {
				//随机分配取派员
				Staff staff = staffList.get(new Random().nextInt(staffList.size()));
				//取派员Id
				workorder.setStaffId(staff.getStaffId());
				//取派员编号
				workorder.setStaffNumber(staff.getStaffNumber());
				//取派员姓名
				workorder.setStaffName(staff.getStaffName());
				//取派员单位
				workorder.setTakeOrgName(staff.getOrgName());
				//取件时间
				workorder.setTakeTime(new Date());
				//定区Id
				workorder.setRipAreaId(ripAreaId);
				//调用添加方法 添加派工单
				businessRecsiptDao.addWorkorder(workorder);
			}
			return result;
		}else {
			return false;
		}
	}
	
	//发送短消息
	public boolean sendMsg() {
		
		return false;
	}

	@Override
	public Subarea findCityCodeByQuery(String mnemoniCode) {
		return businessRecsiptDao.findCityCodeByQuery(mnemoniCode);
	}



	
	
	
	
	
}
